//页面加载
$(document).ready(function(){
    loadGrid();
    $("#username").addClear();
    $("#username").bind("keypress",function(event){
        if(event.keyCode == 13){
            search();
        }
    });
});
var rowHeight = 27;
//加载表格datagrid
function loadGrid() {
    var url;
    var clientH = document.documentElement.clientHeight;
    var offsetH =clientH - 120;
    var pageSize = 10;
    rowHeight = /*(offsetH-55) / pageSize*/27;
    //加载数据
    $('#dataGrid').datagrid({
        width:'100%',
        height:offsetH,
        idField: 'id',
        nowrap: false,
        striped: true,
        title:"用户表",
        border: true,
        loadMsg: "数据加载中，请稍后...",
        collapsible:false,//是否可折叠的
        fit: false,//自动大小
        pagination:true,//分页控件
        rownumbers:true,//行号
        fitcolumns:true,
        url: PATH.BASEPATH+'/admin/sys/user/ajax_user_data',
        method: 'post',
        columns: [[
            {field: 'ck', checkbox: true},
            {field: 'username', title: '用户名',width:'25%',align:'center'},
            {field: 'realname', title: '真实姓名',width:'25%',align:'center'},

            {field: 'status', title: '是否可用',width:'30%',align:'center',
                formatter: function (value, row, index) {
                    return value == '0'?'启用':'禁用';
                }},
            {field:'opt',title:'操作',width:'15%',align:'center', formatter:function(value,index) {
            var name = index.username;
                return "<shiro:hasPermission name='sys:user:add'><a class='datagrid_a_btn_primary fa fa-pencil ' style='font-size: 10px' title='修改' onclick='updateUser("+index.id+");'></a></shiro:hasPermission>"+
                    "<shiro:hasPermission name='sys:user:allocation'><a class='datagrid_a_btn_seccess fa fa-user' style='margin-left: 3px;font-size: 10px' title='分配角色' onclick='allocationAuthority(1,\""+index.username+"\");'></a></shiro:hasPermission>"
                }
            }
        ]],
        //列排序时触发方法
        onSortColumn: function (sort, order) {
            search(sort,order);
        },
        onLoadSuccess: function(data){
            var dataGrid = $("#dataGrid");
            autoHeight(dataGrid,rowHeight);
        }
    });

    //设置分页控件
    var p = $('#dataGrid').datagrid('getPager');
    $(p).pagination({
        pageSize: 2147483647,////每页显示的记录条数，默认为10
        beforePageText: '第',//页数文本框前显示的汉字
        afterPageText: '页    共 {pages} 页',
        displayMsg: '共 {total} 条记录',
        showRefresh: false,
        layout:['list','first','prev','links','next','last']
    });
}
function batchDelete(){
    var ids = [];
    var rows = $('#dataGrid').datagrid('getSelections');
    for(var i=0; i<rows.length; i++){
        ids.push(rows[i].id);
    }
    if(ids.length == 0){
        alertx("请至少选择一项",0);
        return ;
    }
    var idStr = ids.join(",");
    layer.confirm('确定删除所选数据吗?', {icon: 3, title:'提示'}, function(index) {
        layer.close(index);
        $.post('user_delete',{"idStr":idStr},function(data){
            if(data == 0){
                alertx('删除成功',1);
                search();
            }else{
                alertx('删除失败',2);
                search();
            }
        });
    });
}

/**
 * 修改用户信息
 */
function updateUser(id){
   /* var ids = [];
    var rows = $('#dataGrid').datagrid('getSelections');
    for(var i=0; i<rows.length; i++){
        ids.push(rows[i].id);
    }
    if(ids.length == 0 || ids.length >1){
        alertx("只能选择一项",0);
        return ;
    }*/
    layer.open({
        type: 2,
        title: '修改用户页面',
        shadeClose: true,
        maxmin: true,
        shade: 0.8,
        area: ['50%', '85%'],
        content: "user_add?id="+id,
        cancel:function (index) {
            layer.close(index);
            search();
        },
        end:function (index) {
            layer.close(index);
            search();
        }
    });

}

/**
 * 添加用户信息
 */
function addUser(){
    layer.open({
        type: 2,
        title: '添加用户页面',
        shadeClose: true,
        maxmin: true,
        shade: 0.8,
        area: ['50%', '85%'],
        content: 'user_add',
        cancel:function (index) {
            layer.close(index);
            search();
        },
        end:function (index) {
            layer.close(index);
            search();
        }
    });

}

/**
 * 重置方法
 */
function reset(){
    $("#username").val("");
    search();
    $("#username").focus();
}

/**
 * 模糊查询方法
 */
function search(sortName,sortValue) {
    $("#dataGrid").datagrid('load', {
        username: $("#username").val(),
        sortName:sortName,
        sortValue:sortValue,
    });
    $('#dataGrid').datagrid('clearSelections');

}


/**
 * 分配权限
 */
function allocationAuthority(id,username){
/*
    console.log("username:",username);
*/
   /* var ids = [];
    var usernames=[];
    var rows = $('#dataGrid').datagrid('getSelections');
    var username = $('#dataGrid').datagrid('getSelections');
    for(var i=0; i<rows.length; i++){
        ids.push(rows[i].id);
        usernames.push(rows[i].username);
    }
    if(ids.length == 0 || ids.length >1){
        alertx("只能选择一项",0);
        return ;
    }*/
    layer.open({
        type: 2,
        title: '分配角色页面',
        shadeClose: true,
        shade: 0.8,
        area: ['40%', '45%'],
        content: "user_allocation?id="+id+"&username="+username,
        cancel:function (index) {
            layer.close(index);
            search();
        },
        end:function (index) {
            layer.close(index);
            search();
        }
    });

}

